Loading [MathJax]/extensions/TeX/AMSmath.js

2015年11月7日 星期六

Uva 10602 Editor Nottoobad

題目來源:https://www.google.com.tw/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0CB0QFjAAahUKEwje6K_ZlP7IAhWHk5QKHcUjDRY&url=https%3A%2F%2Fuva.onlinejudge.org%2Findex.php%3Foption%3Dcom_onlinejudge%26Itemid%3D8%26page%3Dshow_problem%26problem%3D1543&usg=AFQjCNFX5kwdc7Hn6Hu9Jfjsf7_VW1PKUw&sig2=wMLKP9x9wHlpu52-DCMRfA

程式碼:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
using namespace std;
int main(){
int N,n,i,j,sum;//N->有幾筆測資,n->每筆測資有幾個數
scanf("%d",&N);
string st[1001],temp;
while(N--){
scanf("%d",&n);
for(i=0;i<n;i++)
cin>>st[i];
sort(st,st+n);//依字典順序排序
temp = st[0];
sum = st[0].size();
for(i=1;i<n;i++){
for(j=0;j<temp.size();j++)
if(st[i][j]!=temp[j]) break;
sum+=(st[i].size()-j);
temp = st[i];
}
cout<<sum<<endl;//輸出答案
for(i=0;i<n;i++)
cout<<st[i]<<endl;
}
}
view raw uva 10602.cpp hosted with ❤ by GitHub